<{include file="library/rand.html"}>

<{*
<!--
必要配置参数
$blob_length
$blob_callback

demo:
<form action="<{$saveurl}>" method="post" class="form-horizontal zxform zxblockui">
    <input type="hidden" name="id" value="<{$id}>" />
    <input type="hidden" name="backurl" value="<{$backurl}>" />
    <input type="hidden" name="filepath" value=""/>
    <div class="widget-box">
        <div class="widget-title"> <span class="icon"> <i class="icon-align-justify"></i> </span>
            <h5>上传视频</h5>
        </div>
        <div class="widget-content nopadding">
            <div class="upload_btn ignore_error support_drag">
                <dl class="file_select">
                    <dt></dt>
                    <dd>
                        <h3>选择视频</h3>
                        <p>上传过程中请耐心等待</p>
                    </dd>
                </dl>
                <input class="ignore input_common" type="file" id="file" accept="video/mp4"/>
            </div>
            <div id="blob_progress">
                <div id="blob_finish" style="width: 0%;" progress="0"></div>
            </div>
            <script>
                function blob_callback(res){
                    $(".form input[name=filepath]").val(res.file_path);
                    $(".form").submit();
                }
            </script>
        </div>
    </div>
</form>
-->
*}>

<style>
    .upload_btn.ignore_error .error {
        display:none !important;
    }

    #blob_progress {
        width: 98%;
        height: 40px;
        margin: 1em;
        background-color: #f7f7f7;
        box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
        border-radius: 4px;
        background-image: linear-gradient(to bottom, #f5f5f5, #f9f9f9);
        display: none;
    }

    #blob_finish {
        background-color: #149bdf;
        background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
        background-size: 40px 40px;
        transition: all 0.7s linear 0s;
        -moz-transition: all 0.7s linear 0s; /* Firefox 4 */
        -webkit-transition: all 0.7s linear 0s; /* Safari 和 Chrome */
        -o-transition: all 0.7s linear 0s; /* Opera */
        height: 100%;
    }

    .upload_btn {
        font-size: 0;
        position: relative;
        display: block;
        width: 280px;
        height: 70px;
        margin: 25px auto;
        -webkit-user-select: none;
        -moz-user-select: none;
        user-select: none;
        text-align: center;
        color: #fff;
        border-radius: 2px;
        background-color: #2fb3ff;
        -o-user-select: none;
    }

    .upload_btn * {
        margin: 0;
    }

    .upload_btn:hover {
        color: #fff;
        background-color: #3bb7fc;
    }

    .upload_btn:active {
        color: #fff;
        background-color: #2fb3ff;
        box-shadow: inset 0 0 3px rgba(0, 0, 0, .2);
    }

    .upload_btn .file_select {
        display: inline-block;
        overflow: hidden;
    }

    .upload_btn .file_select dt {
        background-image: url();
        background-repeat: no-repeat;
    }

    .upload_btn .file_select dt {
        float: left;
        width: 32px;
        height: 32px;
        margin-top: 19px;
        background-position: 0 -32px;
    }

    .upload_btn .file_select dd {
        float: left;
        width: 200px;
    }

    .support_drag .file_select h3 {
        padding-top: 10px;
        line-height:30px;
    }

    .upload_btn .file_select h3 {
        font-size: 22px;
        padding-top: 20px;
        -webkit-transition: all .5s;
        -moz-transition: all .5s;
        -o-transition: all .5s;
        transition: all .5s;
    }

    .support_drag .file_select h3 {
        padding-top: 10px;
    }

    .upload_btn .file_select p {
        font-size: 13px;
        line-height: 20px;
        display: block;
    }

    .upload_btn #file {
        position: absolute;
        top: 0;
        left: 0;
        width: 100% !important;
        height: 70px !important;
        opacity: 0;
        cursor: pointer;
    }
</style>
<script>
    $(function () {
        const LENGTH = <{if $blob_length}><{$blob_length}><{else}>1024 * 1024<{/if}>;
        var start;
        var end;
        var blob;
        var progress;
        var blob_num;
        var is_stop = 0;
        var progressObj = document.getElementById('blob_finish');
        /* 上传框更新，自动上传文件 */
        $("#file").change(function(){
            var file = $("#file")[0].files[0];
            var fileName = file.name;
            var mime = fileName.toLowerCase().substr(fileName.lastIndexOf("."));
            if(mime!=".mp4"){
                alert("请选择mp4格式的视频上传");
                fnUpload.outerHTML=fnUpload.outerHTML;
            }
            var total_blob_num = Math.ceil(file.size / LENGTH);
            submitinit();
            $("#blob_progress").show();
            var file_name = <{if $blob_uppath}>'<{php}>echo basename($this->_tpl_vars["blob_uppath"]);<{/php}>'<{else}>getRandName(file)<{/if}>;
            showBlockMsg(".zxblockui", "");
            formsubmit(file, file_name, total_blob_num);
        });

        /* 上传前，初始化参数 */
        function submitinit(){
            start = 0;
            end = start + LENGTH;
            progress = 0;
            blob_num = 1;
        }

        /* 上传文件分片 */
        function cutFile(file) {
            var file_blob = file.slice(start, end);
            start = end;
            end = start + LENGTH;
            return file_blob;
        };

        /* 定义随机文件名 */
        function getRandName(file){
            var index = file.name.lastIndexOf(".");
            var ext = file.name.substr(index+1);
            return _getRandomString(10) + "." + ext;
        }

        /* 文件上传 */
        function formsubmit(file, file_name, total_blob_num){
            blob = cutFile(file);
            var form_data = new FormData();
            form_data.append("file", blob);
            <{if $blob_uppath}>
            form_data.append('file_path', '<{php}>echo "../".str_replace(basename($this->_tpl_vars["blob_uppath"]), "", $this->_tpl_vars["blob_uppath"]);<{/php}>');
            <{/if}>
            form_data.append('file_name', file_name);
            form_data.append("blob_num",blob_num);
            form_data.append("total_blob_num",total_blob_num);
            blob_num += 1;
            $.ajax({
                url: '<{spUrl c=$G__C a=uploadblob pid=$pid position_id=$position_id}>',
                dataType: 'json',
                type: 'POST',
                data: form_data,
                cache: false,
                processData: false,
                contentType: false,
                beforeSend: function(){
                    return true;
                },
                success:
                function(json, status){
                    if(json.code == '1'){
                        if (total_blob_num == 1) {
                            progress = '100%';
                        } else {
                            progress = Math.min(100, (blob_num / total_blob_num) * 100) + '%';
                        }
                        progressObj.style.width = progress;
                        formsubmit(file, file_name, total_blob_num);
                    }else if(json.code == '2'){
                        <{if $blob_callback}>
                        eval("<{$blob_callback}>(json)");
                        <{/if}>
                    }
                },
                error:
                function(xml,status){
                    hideBlockMsg(".zxblockui", "");
                    showErrorDialog(xml.responseText);
                }
            });
        }
    });
</script>